package com.css.fxfzdzzh.modules.reportManagement.controller;

import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.reportManagement.entity.BreakReport;
import com.css.fxfzdzzh.modules.reportManagement.entity.MultipleReport;
import com.css.fxfzdzzh.modules.reportManagement.param.MultiplePageQueryParam;
import com.css.fxfzdzzh.modules.reportManagement.service.MultipleReportService;
import com.css.fxfzdzzh.modules.reportManagement.service.ReportService;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年05月10日  18:11
 * 滑坡/砂土液化/软土震陷评估报告
 */
@RestController
@RequestMapping("/multiple")
public class MultipleReportController {

    @Resource
    MultipleReportService multipleReportService;

    @Resource
    ReportService reportService;

    /**
     *  滑坡/砂土液化/软土震陷评估报告分页查询
     */
    @GetMapping("/multiplePage")
    public RestResponse multiplePage(MultiplePageQueryParam param, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=multipleReportService.multiplePage(param,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /**
     * 查询报告信息
     */
    @GetMapping("/getReport")
    public RestResponse getReport(@RequestParam(value = "reportId",required = false) String reportId){
        if(StringUtils.isBlank(reportId)){
            return ResponseCreator.createSuccessResponse(new MultipleReport());
        }
        return ResponseCreator.createSuccessResponse(multipleReportService.getReport(reportId));
    }

    /***
     * 报告下载
     * @return
     */
    @PostMapping("/downloadReport")
    public void downloadReport(@RequestBody Map<String,Object> map, HttpServletResponse res){
        String reportId =(String) map.get("reportId");
        String type =(String) map.get("type");
        multipleReportService.downloadReport(reportId,type,res);
    }

    /***
     * 上传报告
     * @param file
     * @return
     */
    @PostMapping("/uploadReport")
    public RestResponse uploadReport(@RequestParam("file") MultipartFile file, @RequestParam("taskNum") String num) throws IOException {
        return multipleReportService.uploadReport(file,num);
    }

//    /**
//     * 分页查询历史报告
//     */
//    @GetMapping("/getHistoryReport")
//    public RestResponse getHistoryReport(@RequestParam("taskNum") String num,@RequestParam("reportType") String reportType, HttpServletRequest request){
//        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
//        int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
//        Map<String,Object> page=reportService.getHistoryReport(num,reportType,curPage,pageSize);
//        return ResponseCreator.createSuccessResponse(page);
//    }

    /***
     * 提交审核
     * @param map
     * @return
     */
    @PostMapping("/submitCheak")
    public RestResponse submitCheak( @RequestBody Map<String,Object> map) throws Exception {
        String reportId =(String) map.get("reportId");
        multipleReportService.submitCheak(reportId);
        return ResponseCreator.createSuccessResponse("审核提交成功");
    }

    /**
     *  滑坡/砂土液化/软土震陷评估报告生成
     */
    @PostMapping("/createMultipleReport")
    public RestResponse createMultipleReport(@RequestBody  Map<String,Object> map){
        try{
            MultipleReport report = multipleReportService.createMultipleReport((String) map.get("taskNum"));
            return ResponseCreator.createSuccessResponse(report);
        }catch (Exception e){
            return ResponseCreator.createFailResponse("报告生成失败");
        }
    }

    /***
     * 获取回退意见
     * @param reportId 报告id
     * @return
     */
    @GetMapping("/getBackReportMsg")
    public RestResponse getBackReport(@RequestParam("reportId") String reportId){
        return ResponseCreator.createSuccessResponse(multipleReportService.getBackReport(reportId));
    }
}
